package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 前端首页菜品展示
 * @author ChuMingYu
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @author ChuMingYu
     */
    @PostMapping ("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.getCurrentId();
         /* 菜品或者套餐是否点过
            如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
            如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
            如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
          */
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            //是菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            //是套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(qw);
        // 第一次点的时候是新增 ，后面在点击的时候是修改
        if(one == null){
            // 如果没有，则是新增
            // 设置 number=1
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
        }else {
            // 如果有，则是更新
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }

        return R.success("添加购物车成功！");
    }

    /**
     * 查询购物车
     * @author ChuMingYu
     */
    @GetMapping("/list")
    public R selectShoppingCart(){
        log.info("查看购物车..");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        qw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(qw);

        return R.success(list);
    }

    /**
     * 取消购物车
     * @author ChuMingYu
     */
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        //菜品或者套餐id 查询出当前ShoppingCart数据
        // 如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
        // 如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            //是菜品
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else{
            //是=套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);

        //判断number >0 / <=0

        if(number > 0 ){
            //更新
            shoppingCartService.updateById(one);
        }else{
            //删除
           shoppingCartService.remove(qw);
        }

        return R.success(one);
    }

    /**
     * 清空购物车
     * @author ChuMingYu
     */
    @DeleteMapping("/clean")
    public R clean(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(qw);
        return  R.success("清空购物车成功");

    }

}
